﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StockMonitor.Configuration;

namespace StockMonitor.Forms
{
    public partial class frmEditETFInPool : Form
    {
        #region Ctor.

        public frmEditETFInPool()
        {
            InitializeComponent();
        }

        public frmEditETFInPool(string stockCode, Action<ETFPoolItem> AddToPool)
            : this()
        {
            UpdatePoolUI = AddToPool;

            StockItem = ETFStockPool.Instance.Pool.Find(s => s.StockCode == stockCode);

            this.lblStockCode.Text = stockCode;
            this.numAmount.Value = StockItem.ExchangeAmount;
        }

        #endregion

        #region Members

        ETFPoolItem StockItem;

        #endregion

        #region Event Handlers

        Action<ETFPoolItem> UpdatePoolUI;

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (numAmount.Value % 100 != 0 || numAmount.Value < 0)
            {
                errorProvider1.SetError(numAmount, "买入数量需是100的倍数");
                return;
            }

            StockItem.ExchangeAmount = Convert.ToInt32(this.numAmount.Value);
            ETFStockPool.Instance.Save();
            UpdatePoolUI(StockItem);

            this.Close();
        }


        #endregion
    }
}
